package com.example.administrator.foodrowfresh.configs.classifyconfing.classifyentity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.foodrowfresh.R;
import com.example.administrator.foodrowfresh.configs.classifyconfing.MyClassifyConfing;
import com.example.administrator.foodrowfresh.configs.classifyconfing.classifyadapter.MyClassifyViewPagerAdapter;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class MyClassifyDetailItemActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private List<Drawable> drawableList;
    private String itemId;
    private MyClassifyViewPagerAdapter viewPagerAdapter;
    private MyClassifyDetailItemEntity.DataBean data;
    //查找详情页的控件：
    private TextView tv_name,last_time_info,rate_price,rate_weight,old_price_name,old_price,old_price_weight,typeName,Descrption;
    private TextView serverName3,serverName2,serverName1,goodsNumber,place,store;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_classify_detail_item);
        //初始化shareSDK
        ShareSDK.initSDK(this);
        initView();
        initData();
    }

    private void initView() {
        mViewPager = (ViewPager) this.findViewById(R.id.Classify_detail_item_viewpager);
        //查询控件，一边填数据
        tv_name= (TextView) this.findViewById(R.id.promotion_content_activity_tv_name);
        last_time_info= (TextView) this.findViewById(R.id.promotion_content_activity_tv_last_time_info);
        rate_price= (TextView) this.findViewById(R.id.promotion_content_activity_tv_sx_price);
        rate_weight= (TextView) this.findViewById(R.id.promotion_content_activity_tv_sp_weight);
        old_price= (TextView) this.findViewById(R.id.promotion_content_activity_tv_old_price);
        old_price_weight= (TextView) this.findViewById(R.id.promotion_content_activity_tv_old_price_weight);
        typeName= (TextView) this.findViewById(R.id.promotion_content_activity_bv_TypeName);
        Descrption= (TextView) this.findViewById(R.id.promotion_content_activity_tv_TypeName);
        serverName3= (TextView) this.findViewById(R.id.promotion_content_activity_bt_Name3);
        serverName2= (TextView) this.findViewById(R.id.promotion_content_activity_bt_Name2);
        serverName1= (TextView) this.findViewById(R.id.promotion_content_activity_bt_Name1);
        goodsNumber= (TextView) this.findViewById(R.id.promotion_content_activity_tv_number);
        place= (TextView) this.findViewById(R.id.promotion_content_activity_tv_place);
        store= (TextView) this.findViewById(R.id.promotion_content_activity_tv_store);

    }

    private void initData() {
        data = new MyClassifyDetailItemEntity.DataBean();
        drawableList = new ArrayList<>();
        Intent intent = getIntent();
        //获取条目的ID号
        itemId = intent.getStringExtra("id");
        //获取适配器数据
        getBitmap();
    }

    public void getBitmap() {
        RequestParams params = new RequestParams(MyClassifyConfing.Item_first + itemId + MyClassifyConfing.Item_end);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //          Toast.makeText(MyClassifyDetailItemActivity.this,result, Toast.LENGTH_LONG).show();
                //解析数据
                Gson gson = new Gson();
                MyClassifyDetailItemEntity myClassifyDetailItemEntity = gson.fromJson(result, MyClassifyDetailItemEntity.class);
                data = myClassifyDetailItemEntity.getData();
             //   Toast.makeText(MyClassifyDetailItemActivity.this, data.getBanner().get(0).getImage(), Toast.LENGTH_LONG).show();
                //配置适配器
                viewPagerAdapter = new MyClassifyViewPagerAdapter(data, MyClassifyDetailItemActivity.this);
                //设置适配器
                mViewPager.setAdapter(viewPagerAdapter);
                //设置值
                tv_name.setText(data.getProduct().getName() + "");
                last_time_info.setText(data.getProduct().getLast_time_info() + "");
                rate_price.setText(data.getProduct().getRate_price() + "");
                rate_weight.setText(data.getProduct().getRate_weight() + "");
                old_price.setText(data.getProduct().getOld_price() + "");
                old_price_weight.setText(data.getProduct().getOld_price_weight() + "");
                typeName.setText(data.getPromotionalActivities().get(0).getTypeName());
                Descrption.setText(data.getPromotionalActivities().get(0).getDescrption());
                if (data.getProductserives().size() >= 0) {
                    serverName3.setText(data.getProductserives().get(0).getName());
                }
                if (data.getProductserives().size() >= 1) {
                    serverName2.setText(data.getProductserives().get(1).getName());
                }
                if (data.getProductserives().size() >= 2) {
                    serverName1.setText(data.getProductserives().get(2).getName());
                }
                goodsNumber.setText(data.getProduct().getNumber() + "");
                place.setText(data.getProduct().getPlace() + "");
                store.setText(data.getProduct().getStore() + "");
               // Toast.makeText(MyClassifyDetailItemActivity.this, "走到刷新适配器的步骤", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(MyClassifyDetailItemActivity.this, "网络访问不成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                case R.id.promotion_item_activity_bt_share:
                    showShare(false, null);
                    break;
                case R.id.promotion_item_activity_bt_back:
                    finish();
                    break;
            }
        }
    }

    // 使用快捷分享完成分享
    private void showShare(boolean silent, String platform) {
        final OnekeyShare oks = new OnekeyShare();
// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        //分享标题
        oks.setTitle("#name#");
        oks.setTitleUrl("http://www.baidu.com");
        oks.setComment("....");
        //分享图片
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/05/21/oESpJ78_533x800.jpg");
        oks.setSilent(silent);
        if (platform != null) {
            oks.setPlatform(platform);
        }

        oks.show(this);
    }

    //如果activity被销毁，就停止shareSDK
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(this);
    }


}
